package com.yyplatform.admin.platform.notice.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import com.yyplatform.admin.platform.notice.model.entity.SysNoticeEntity;
import org.springframework.data.repository.query.Param;

/**
 * Created on 2024-09-14
 */
public interface SysNoticeRepository extends JpaRepository<SysNoticeEntity, Long>,
        JpaSpecificationExecutor<SysNoticeEntity> {

    @Query(
            value = "SELECT * FROM sys_notice WHERE 1=1 "
                    + " AND if(:title != '' , `title` LIKE concat('%', title, '%'), 1=1 )  "
                    + " AND if(:sendFlag IS NOT NULL , `send_flag` = :sendFlag, 1=1 )  "
                    + " AND if(:noticeType IS NOT NULL , `notice_type` = :noticeType, 1=1 )  "
                    + " AND if(:deleteFlag IS NOT NULL , `delete_flag` =:deleteFlag, 1=1 )  "
                    + " AND (create_time >= :beginDate AND create_time <= :endDate) "
            ,
            countQuery = "SELECT count(id) FROM sys_notice WHERE 1=1 "
                    + " AND if(:title != '' , `title` LIKE concat('%', title, '%'), 1=1 )  "
                    + " AND if(:sendFlag IS NOT NULL , `send_flag` = :sendFlag, 1=1 )  "
                    + " AND if(:noticeType IS NOT NULL , `notice_type` = :noticeType, 1=1 )  "
                    + " AND if(:deleteFlag IS NOT NULL , `delete_flag` =:deleteFlag, 1=1 )  "
                    + " AND (create_time >= :beginDate AND create_time <= :endDate) "
            ,
            nativeQuery = true)
    Page<SysNoticeEntity> findPage(
            @Param("title") String title, //1
            @Param("sendFlag") Boolean sendFlag, //2
            @Param("noticeType")  Integer noticeType,//3
            @Param("deleteFlag")  Boolean deleteFlag,//4
            @Param("beginDate") Date beginDate, //5
            @Param("endDate")  Date endDate,//6
            PageRequest pageRequestOrderByIdDesc);


}
